const { app, BrowserWindow } = require('electron')
const path = require('path')
const glob = require('glob')
const isDev = require('electron-is-dev')
const gotLock = app.requestSingleInstanceLock()



const localUrl = 'http://localhost:3000'

let win
let tomato
function createWindow() {
    win = new BrowserWindow({
        width: 800,
        height: 600,
        show: false,
        webPreferences: {
            javascript: true,
            plugins: true,
            nodeIntegration: false, // 是否集成 Nodejs 安全隐患
            webSecurity: false,
            preload: path.join(__dirname, 'preload.js')
        }
    })
    win.loadURL(localUrl)
    win.webContents.openDevTools()
    // win.loadURL('http://120.78.80.132:8080/#/')
    win.once('ready-to-show', () => {
        tomato.hide()
        win.show()
        win.maximize()
    })
    win.on('closed', () => {
        win = null
        tomato = null
    })
}

function createTomato() {
    tomato = new BrowserWindow({
        width: 400,
        height: 100,
        webPreferences: {
            javascript: true,
            plugins: true,
            nodeIntegration: false, // 是否集成 Nodejs 安全隐患
            webSecurity: false,
        }
    })
    tomato.loadFile('./index.html')
}

if (!gotLock) {
    app.quit()
} else {
    app.on('second-instance', () => {
        win.show()
    })
}

app.on('ready', createWindow)
app.on('ready', createTomato)

// 当全部窗口关闭时退出。
app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit();
    }
});

app.on('activate', () => {
    if (win === null) {
        createWindow();
        createDevTools()
    }
});



loadDemos();
// Require each JS file in the main-process dir
function loadDemos() {
    console.log(path.join(__dirname, 'main-process/**/*.js'))
    const files = glob.sync(path.join(__dirname, 'main-process/**/*.js'))
    files.forEach((file) => { require(file) })
}